// 应用配置文件 - 统一管理所有配置项
export const APP_CONFIG = {
  // 应用基本信息
  app: {
    name: process.env.NEXT_PUBLIC_APP_NAME || '奥数学院',
    description: process.env.NEXT_PUBLIC_APP_DESCRIPTION || '中小学生奥数学习平台',
    version: '1.0.0',
    author: '奥数学院开发团队'
  },

  // 服务器配置
  server: {
    port: process.env.PORT || 3000,
    host: process.env.HOST || 'localhost',
    baseUrl: process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
  },

  // 题库配置
  questions: {
    questionsPerTopic: parseInt(process.env.QUESTIONS_PER_TOPIC || '50'),
    totalTopics: parseInt(process.env.TOTAL_TOPICS || '60'),
    difficultyLevels: 5,
    difficultyDistribution: {
      1: 0.20, // 20% 基础题
      2: 0.30, // 30% 标准题
      3: 0.30, // 30% 进阶题
      4: 0.16, // 16% 提高题
      5: 0.04  // 4% 竞赛题
    }
  },

  // 演示模式配置
  demo: {
    enabled: process.env.NODE_ENV === 'development' || process.env.DEMO_MODE === 'true',
    accounts: {
      student: {
        username: process.env.DEMO_STUDENT_USERNAME || 'demo_student',
        password: process.env.DEMO_STUDENT_PASSWORD || 'demo123',
        email: 'student@demo.com',
        school: '演示小学',
        grade: 5
      },
      admin: {
        username: process.env.DEMO_ADMIN_USERNAME || 'admin',
        password: process.env.DEMO_ADMIN_PASSWORD || 'admin@123',
        email: 'admin@mathacademy.com',
        school: '系统管理',
        grade: 0
      }
    }
  },

  // 路由配置
  routes: {
    home: '/',
    login: '/login',
    register: '/register',
    dashboard: '/dashboard',
    learn: '/learn',
    practice: {
      base: '/practice',
      daily: '/practice/daily',
      free: '/practice/free',
      challenge: '/practice/challenge',
      session: '/practice/session'
    },
    admin: '/admin'
  },

  // 学习配置
  learning: {
    gradeRange: {
      primary: [1, 2, 3, 4, 5, 6],
      middle: [7, 8, 9],
      high: [10, 11, 12]
    },
    categories: {
      primary: ['基础计算', '行程问题', '几何图形', '数论基础', '逻辑推理', '应用问题', '组合计数'],
      middle: ['代数基础', '函数', '几何基础', '数论进阶', '组合数学', '概率统计'],
      high: ['高等代数', '解析几何', '微积分基础', '数学建模']
    }
  },

  // 练习配置
  practice: {
    dailyQuestionCount: parseInt(process.env.DAILY_QUESTION_COUNT || '10'),
    sessionTimeLimit: parseInt(process.env.SESSION_TIME_LIMIT || '1800'), // 30分钟
    maxHints: parseInt(process.env.MAX_HINTS || '2'),
    passingScore: parseFloat(process.env.PASSING_SCORE || '0.6') // 60%及格
  },

  // 数据库配置
  database: {
    mysql: {
      host: process.env.DB_HOST || 'localhost',
      port: parseInt(process.env.DB_PORT || '3306'),
      database: process.env.DB_NAME || 'math_academy',
      user: process.env.DB_USER || 'root',
      password: process.env.DB_PASSWORD || ''
    }
  },

  // 认证配置
  auth: {
    sessionTimeout: parseInt(process.env.SESSION_TIMEOUT || '86400'), // 24小时
    passwordMinLength: parseInt(process.env.PASSWORD_MIN_LENGTH || '6'),
    usernameMinLength: parseInt(process.env.USERNAME_MIN_LENGTH || '3')
  },

  // 开发配置
  development: {
    enableDebug: process.env.NODE_ENV === 'development',
    enableMockData: process.env.ENABLE_MOCK_DATA === 'true',
    logLevel: process.env.LOG_LEVEL || 'info'
  }
};

// 辅助函数
export const getBaseUrl = () => APP_CONFIG.server.baseUrl;
export const isDemoMode = () => APP_CONFIG.demo.enabled;
export const isProduction = () => process.env.NODE_ENV === 'production';
export const isDevelopment = () => process.env.NODE_ENV === 'development';

// 题库相关辅助函数
export const getQuestionsPerTopic = () => APP_CONFIG.questions.questionsPerTopic;
export const getTotalTopics = () => APP_CONFIG.questions.totalTopics;
export const getTotalQuestions = () => APP_CONFIG.questions.questionsPerTopic * APP_CONFIG.questions.totalTopics;

// 路由辅助函数
export const getRoute = (routeName: string) => {
  const routes = APP_CONFIG.routes as any;
  return routes[routeName] || '/';
};

export default APP_CONFIG;
